// pages/users/users.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [1, 2, 3]
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.refresh();
  },
  
  uppertieba(e) {
    var that = this;
    var articleId = e.target.dataset.id;
    var { index } = e.currentTarget.dataset;
    var list = that.data.list;
    var dataform = {
      "articleId": articleId,
      "state":1
    };
    wx.showModal({
      title: '提示',
      content: '您确认要上架这个帖子吗？',
      showCancel:true,
      success:function(res){
        console.log("上架成功")
        wx.request({
          url: app.ipaddress + 'modifyArticle',
          method: 'POST',
          data: JSON.stringify(dataform),
          header: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            var result = res.data.success;
            var toastText = '审核完成';
            if (result) {
              that.refresh();
            } else {
              toastText = '操作失败' + res.data.errMsg;
            }
            console.log(res.data.errMsg)
            wx.showToast({
              title: toastText,
              icon: '',
              duration: 2000
            })
          }
        })
      },
      fail:function(res){
        console.log("上架取消")
      }
    })
    
  },
  lowertieba(e) {
    var that = this;
    console.log(e)
    var articleId = e.target.dataset.id;
    var { index } = e.currentTarget.dataset;
    var list = that.data.list;
    var dataform = {
      "articleId": articleId,
      "state": 0
    };
    wx.showModal({
      title: '提示',
      content: '您确认要下架这个帖子吗？',
      showCancel: true,
      success: function (res) {
        console.log("下架成功")
        wx.request({
          url: app.ipaddress + 'modifyArticle',
          method: 'POST',
          data: JSON.stringify(dataform),
          header: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            var result = res.data.success;
            var toastText = '审核完成';
            if (result) {
              that.refresh();
            } else {
              toastText = '操作失败' + res.data.errMsg;
            }
            console.log(res.data.errMsg)
            wx.showToast({
              title: toastText,
              icon: '',
              duration: 2000
            })
          }
        })
      },
      fail: function (res) {
        console.log("下架取消")
      }
    }) 
  },
  refresh:function(){
    var that = this;
    console.log(app.ipaddress)
    wx.request({
      url: app.ipaddress + 'getAllArticleList',
      method: 'GET',
      data: {},
      success: function (res) {
        var list = res.data.list;
        if (list == null) {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            list: list
          });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})